<?php defined('BASEPATH') OR exit('No direct script access allowed');

class manageads extends wsBackend
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('ads_model');
    }

    public function index()
    {
        $data = array();

        $this->tpl->meta ['title'] = '广告管理 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '广告管理';
        $this->tpl->page_header_ = '';
        $this->tpl->breadcrumbs['广告管理'] = 'active';


        if ($this->input->get('ad_name'))
            $filter['name'] = $this->input->get('ad_name');

        $config['uri_segment'] = 4;
        $data['offset'] = $filter['offset'] = $this->uri->segment($config['uri_segment']);
        $config['base_url'] = preg_replace('/' . $this->config->item('url_suffix') . '$/ix', '', admin_url('manageAds/index'));
        $config['per_page'] = $filter['per_page'] = $this->config->item('items_per_page');

        if (!empty($_GET)) {
            $get_method = '/?' . http_build_query($this->input->get());
            $config['suffix'] = $this->config->item('url_suffix') . $get_method;
            $config['first_url'] = $config['base_url'] . $get_method;
        } else {
            $config['suffix'] = $this->config->item('url_suffix');
        }
        $this->tpl->page_header_ = $config['total_rows'] = $this->ads_model->get_ads($filter + array('count' => true));
        $config['use_page_numbers'] = FALSE;
        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();
        $data['ads_list'] = $this->ads_model->get_ads($filter);


        $this->tpl->contents [] = $this->tpl->fetch('contents/ads_index', $data);
        $this->tpl->render();
    }


    public function add()
    {
        $data = array();

        $this->tpl->meta ['title'] = '添加广告 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '添加广告';
        $this->tpl->page_header_ = '';
        $this->tpl->breadcrumbs['添加广告'] = admin_url('manageAds');
        $this->tpl->breadcrumbs['添加广告'] = 'active';
        $data['ad'] = false;


        $this->load->library('form_validation');
        $this->form_validation->set_rules('ad_name', 'Ad Name', 'required|trim');

        if ($this->form_validation->run()) {
            $insert_data = array(
                'ad_name' => $this->input->post('ad_name'),
                'ad_code' => $this->input->post('ad_code'),
            );
            $insert_id = $this->ads_model->insert($insert_data);

            if ($insert_id) {
                $this->language->save_ads();
                $this->session->set_flashdata('SUCCESS', $this->input->post('ad_name') . ' - 添加成功');
                redirect(admin_url('manageAds'));
            }
        }

        $this->tpl->contents [] = $this->tpl->fetch('contents/ads_update', $data);
        $this->tpl->render();
    }

    public function edit($id = '', $index = '')
    {
        if (!$id)
            redirect(admin_url('manageAds/index/' . $index));
        $data = array();


        $this->tpl->meta ['title'] = '编辑广告 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '编辑广告';
        $this->tpl->page_header_ = '';
        $this->tpl->breadcrumbs['Manage Ads'] = admin_url('manageAds');
        $this->tpl->breadcrumbs['编辑广告'] = 'active';
        $data['ad'] = $this->ads_model->get_ad($id);
        if (!$data['ad'])
            redirect(admin_url('manageAds/index/' . $index));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('ad_name', 'Ad Name', 'required|trim');

        if ($this->form_validation->run()) {
            $update_data = array(
                'ad_name' => $this->input->post('ad_name'),
                'ad_code' => $this->input->post('ad_code'),
            );
            $this->ads_model->update($update_data, array('ad_seq_id' => $id));
            $this->session->set_flashdata('SUCCESS', $this->input->post('ad_name') . ' - 编辑成功');
            $this->language->save_ads();
            redirect(admin_url('manageAds/edit/' . $id . '/' . $index));
        }

        $this->tpl->contents [] = $this->tpl->fetch('contents/ads_update', $data);
        $this->tpl->render();
    }

    public function delete($id = '', $index = '')
    {
        if ($id) {
            $this->ads_model->delete($id);
            $this->language->save_ads();
            $this->session->set_flashdata('SUCCESS', $id . ' - 删除成功');
        }
        redirect(admin_url('manageAds/index/' . $index));
    }
}
